package cn.idongjia.live.exception;

import cn.idongjia.exception.Result;
import cn.idongjia.exception.ResultCodes;
import com.google.gson.Gson;

import javax.ws.rs.core.Response;
import java.io.Serializable;

/**
 * 异常基础抽象类
 * Created by wuqiang on 16/4/24.
 */
public abstract class BaseException extends RuntimeException implements Serializable {

    protected static final Gson GSON = new Gson();

    private Integer code;
    private String msg;

    public BaseException() {
    }

    public BaseException(int code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public BaseException(String message) {
        super(message);
        this.msg = message;
        this.code = ResultCodes.FAILED.getCode();
    }

    public BaseException(Result result) {
        super(result.getMsg());
        this.code = result.getCode();
        this.msg = result.getMsg();
    }

    public BaseException(String message, Throwable cause) {
        super(message, cause);
    }

    public BaseException(Throwable cause) {
        super(cause);
    }

    public BaseException(String message, Throwable cause,
                         boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Result getResult() {
        return new Result(code, msg);
    }

    public Response.Status getStatus() {
        return Response.Status.INTERNAL_SERVER_ERROR;
    }

    @Override
    public String toString() {
        if (this.msg != null) {
            return GSON.toJson(this);
        }
        return super.toString();
    }
}
